1805D - A Wide Wide Graph - CodeForces Solution


dfs and similar dp graphs trees

Please click on ads to support us..

Python Code:

n = int(input())
slovar = {}
for i in range(1, n + 1):
    slovar[i] = []
for i in range(n - 1):
    a, b = map(int, input().split())
    slovar[a].append(b)
    slovar[b].append(a)
from collections import deque

a = deque()
cash = set()
a.append(1)
cash.add(1)
while len(cash) != n:
    k = len(a)
    mass = []
    while k != 0:
        k -= 1
        for i in slovar[a.popleft()]:
            if i not in cash:
                cash.add(i)
                a.append(i)
                mass.append(i)
a = deque()
cash = set()
a.append(mass[0])
cash.add(mass[0])
z1 = mass[0]
s = 0
op = {}
while len(cash) != n:
    k = len(a)
    mass = []
    while k != 0:
        k -= 1
        x = a.popleft()
        for i in slovar[x]:
            if i not in cash:
                cash.add(i)
                a.append(i)
                mass.append(i)
                op[i] = x
    s += 1
z2 = mass[0]
i = z2
otvet = [z2]
while i != z1:
    otvet.append(op[i])
    i = op[i]
if s % 2 == 0:
    cash = set()
    main = otvet[s // 2]
    stack = deque()
    stack.append(main)
    cash.add(main)
    vivod = []
    for i in range(1, n + 1):
        if i <= s // 2:
            vivod.append(1)
        elif i > s:
            vivod.append(n)
        else:
            if i == s // 2 + 1:
                vivod.append(2)
            else:
                k = len(stack)
                while k != 0:
                    k -= 1
                    x = stack.popleft()
                    for i in slovar[x]:
                        if i not in cash:
                            cash.add(i)
                            stack.append(i)
                vivod.append(1 + len(cash))
    print(' '.join(str(i) for i in vivod))


else:
    cash2 = set()
    main1, main2 = otvet[s // 2], otvet[s // 2 + 1]
    stack = deque()
    stack.append(main1)
    cash2.add(main1)
    cash2.add(main2)
    stack.append(main2)
    vivod = []
    for i in range(1, n + 1):
        if i <= s // 2 + 1:
            vivod.append(1)
        elif i > s:
            vivod.append(n)
        else:
            if i == s // 2 + 2:
                vivod.append(3)
            else:
                k = len(stack)
                while k != 0:
                    k -= 1
                    x = stack.popleft()
                    for i in slovar[x]:
                        if i not in cash2:
                            cash2.add(i)
                            stack.append(i)
                vivod.append(1 + len(cash2))
    print(' '.join(str(i) for i in vivod))

C++ Code:

#include<bits/stdc++.h>
#define inf 1e18
#define mod 998244353
using namespace std;

int dis[100005];
int d[100005];
vector<int> g[100005];
int ans[100005];
int c=0;

void dfs(int u,int pa){
    for(auto v:g[u]){
        if(v==pa) continue;
        dis[v]=dis[u]+1;
        if(dis[v]>dis[c]) c=v;
        dfs(v,u);
    }
}

void solve(){
    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    dfs(1,0);
    dis[c]=0;
    dfs(c,0);
    for(int i=1;i<=n;i++){
        d[i]=max(d[i],max(dis[i],dis[c]-dis[i]));
    }
    dis[c]=0;
    dfs(c,0);
    for(int i=1;i<=n;i++){
        d[i]=max(d[i],max(dis[i],dis[c]-dis[i]));
        ans[d[i]]++;
    }
//    for(int i=1;i<=n;i++){
//        cout<<d[i]<<" ";
//    }
    int now=1;
    for(int i=0;i<n;i++){
        now+=ans[i];
        if(now<n) cout<<now<<" ";
        else cout<<n<<" ";
    }
    cout<<endl;
}


int main(){
//    ios::sync_with_stdio(false);
//    cin.tie(nullptr);
//        int n;
//        cin>>n;
//        while(n--){
            solve();
//        }
};


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations